﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class ChiTietKhamBenhDAL
    {
        PhongMachTuDBDataContext db = new PhongMachTuDBDataContext();
        public int tinhTienThuoc(int maKhamBenh)
        {
     
            var q = from c in db.CHITIETKHAMBENHs
                    join p in db.DONGIAs
                    on new { a = c.MaThuoc } equals new { a = p.MaThuoc }
                    where c.MaKhamBenh == maKhamBenh
                    select new
                    {
                        c.MaThuoc,
                        c.MaDonVi,
                        c.SoLuong,
                        p.DonGia1,
                        TienThuoc = c.SoLuong * p.DonGia1,
                    };


            int nextID = 0;
            foreach (var item in q)
            {
                nextID = nextID + item.TienThuoc;
            }
            return nextID;

        }
        public List<CHITIETKHAMBENH> LayDSKhamBenh()
        {
          
            var q = from s in db.CHITIETKHAMBENHs
                    select s;
            return q.ToList();
        }
        public List<CHITIETKHAMBENH> LayDSKhamBenh(int maKB)
        {

            var q = from s in db.CHITIETKHAMBENHs
                    where s.MaKhamBenh == maKB
                    select s;
            return q.ToList();
        }
        public IQueryable LayDSThuocTheoPKB(int maKB)
        {
  
            var q = from c in db.CHITIETKHAMBENHs
                    join p in db.DONGIAs on c.MaThuoc equals p.MaThuoc
                    join e in db.THUOCs on c.MaThuoc equals e.MaThuoc
                    join dv in db.DONVIs on c.MaDonVi equals dv.MaDonVi
                    join cd in db.CACHDUNGs on c.MaCachDung equals cd.MaCachDung
                    where c.MaKhamBenh == maKB
                    select new
                    {
                        THUOC=e.TenThuoc,
                        DONVI=dv.TenDonVi,
                        c.SoLuong,
                        CACHDUNG=cd.TenCachDung,
                    };
            return q;
        }
        public IQueryable LayDSCTKB(int maKB)
        {

            var q = from c in db.CHITIETKHAMBENHs
                    join p in db.DONGIAs on c.MaThuoc equals p.MaThuoc
                    join e in db.THUOCs on c.MaThuoc equals e.MaThuoc
                    join dv in db.DONVIs on c.MaDonVi equals dv.MaDonVi
                    join cd in db.CACHDUNGs on c.MaCachDung equals cd.MaCachDung
                    where c.MaKhamBenh == maKB
                    select new
                    {
                        e.TenThuoc,

                        dv.TenDonVi,
                        c.SoLuong,
                        cd.TenCachDung,
                    };
            return q;
        }
        public List<CHITIETKHAMBENH> LayDSCTKB_TEN(int maKB,string tenThuoc,string tenDV)
        {

            var q = from c in db.CHITIETKHAMBENHs
                    join p in db.DONGIAs on c.MaThuoc equals p.MaThuoc
                    join e in db.THUOCs on c.MaThuoc equals e.MaThuoc
                    join dv in db.DONVIs on c.MaDonVi equals dv.MaDonVi
                    join cd in db.CACHDUNGs on c.MaCachDung equals cd.MaCachDung
                    where c.MaKhamBenh == maKB && c.THUOC.TenThuoc == tenThuoc && c.DONVI.TenDonVi == tenDV
                    select c;
            return q.ToList();
        }
        public IQueryable LapPhieuKhamBenh()
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.KHAMBENHs
                    select new
                    {
                        s.BENHNHAN.HoTen,
                        s.LOAIBENH.TenLoaiBenh,
                        s.MaKhamBenh,
                        s.MaBenhNhan,
                        s.MaLoaiBenh,
                        s.NgayKham,
                        s.TrieuChung

                    };
            return q;
        }
        public bool ThemChiTietKhamBenh(CHITIETKHAMBENH s)
        {
            try
            {                
                    CHITIETKHAMBENH r = new CHITIETKHAMBENH();
                    r.MaDonVi = s.MaDonVi;
                    r.MaKhamBenh = s.MaKhamBenh;
                    r.MaCachDung = s.MaCachDung;
                    r.MaThuoc = s.MaThuoc;
                    r.SoLuong = s.SoLuong;
                    db.CHITIETKHAMBENHs.InsertOnSubmit(r);
                    db.SubmitChanges();
                    return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaChiTietKhamBenh(CHITIETKHAMBENH s)
        {
            try
            {
          
                CHITIETKHAMBENH r = db.CHITIETKHAMBENHs.Single(id => id.MaChiTietKhamBenh == s.MaChiTietKhamBenh);
                r.MaDonVi = s.MaDonVi;
                r.MaKhamBenh = s.MaKhamBenh;
                r.MaThuoc = s.MaThuoc;
                r.SoLuong = s.SoLuong;
                r.MaCachDung = s.MaCachDung;
        
                db.SubmitChanges();
                return true;

            }
            catch
            {
        
                return false;
            }
        }
        public bool xoaChiTietKhamBenh(int MaChiTietKhamBenh)
        {
            try
            {
                var delete = from d in db.CHITIETKHAMBENHs
                             where d.MaChiTietKhamBenh == MaChiTietKhamBenh
                             select d;
                if (delete.Count() > 0)
                {
                    db.CHITIETKHAMBENHs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool xoaChiTietKhamBenh(int maKB, string tenThuoc, string tenDV)
        {
            try
            {
                var q = from c in db.CHITIETKHAMBENHs
                        join p in db.DONGIAs on c.MaThuoc equals p.MaThuoc
                        join e in db.THUOCs on c.MaThuoc equals e.MaThuoc
                        join dv in db.DONVIs on c.MaDonVi equals dv.MaDonVi
                        join cd in db.CACHDUNGs on c.MaCachDung equals cd.MaCachDung
                        where c.MaKhamBenh == maKB && c.THUOC.TenThuoc == tenThuoc && c.DONVI.TenDonVi == tenDV
                        select c;
                if (q.Count() > 0)
                {
                    db.CHITIETKHAMBENHs.DeleteOnSubmit(q.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
